<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="id:" prop="id.value">
                            <el-input v-model="queryParams.id.value" placeholder="请输入id" clearable />
                        </el-form-item>
                    </el-col> -->
                    <el-col :md="12" :sm="12">
                        <el-form-item label="广告账户:" prop="advertiser_id.value">
                                <el-select
                                    v-model="queryParams.advertiser_id.value"
                                    filterable
                                    multiple
                                    collapse-tags
                                    style="min-width:300px"
                                    placeholder="选择广告账户(可模糊搜索)">
                                        <el-option
                                            v-for="item in advertiser_arr"
                                            :key="item.id"
                                            :label="item.advertiser_id"
                                            :value="item.id"
                                            >
                                            <span style="float: left">{{ item.advertiser_id }}</span>
                                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.advertiser_name }}</span>
                                        </el-option>
                                </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="月份:" prop="month.value">
                            <el-date-picker
                                v-model="queryParams.month.value"
                                type="month"
                                placeholder="选择月份">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :md="12" :sm="12">
                        <el-form-item label="时间段:" prop="datetime.value">
                            <el-date-picker
                                v-model="queryParams.datetime.value"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始"
                                end-placeholder="结束"
                                align="right"
                                value-format="yyyy-MM-dd HH:mm:ss"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="广告账户:" prop="advertiser_id.value">
                            <el-input v-model="queryParams.advertiser_id.value" placeholder="请输入广告账户" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="广告名称:" prop="advertiser_name.value">
                            <el-input v-model="queryParams.advertiser_name.value" placeholder="请输入广告名称" clearable />
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="店铺:" prop="site_id.value">
                            <SelectPage v-model="queryParams.site_id.value" :url="'/admin/site/selectPage'" show_field="name" query_field="name" :multiple="false" />
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :md="6" :sm="12">
                        <el-form-item label="店铺:" prop="site_id.value">
                            <SelectPage v-model="queryParams.site_id.value" :url="'/admin/site/selectPage'" show_field="name" query_field="name" :multiple="false" />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="运营:" prop="admin_id.value">
                            <SelectPage v-model="queryParams.admin_id.value" :url="'/admin/system.admin/selectPage'" show_field="nickname" query_field="nickname" :filter="{group_id:{value:6,op:'='}}" :multiple="false" />
                        </el-form-item>
                    </el-col> -->

                    <el-col :md="6" :sm="12">
                        <el-form-item class="" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <!-- <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button> -->
                </el-col>
                <!-- <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

            <!-- <p>当前统计
                <span style="font-size: 20px;color:#fd2f2f;">{{start_time}}</span>  
                的广告数据。

                <el-date-picker
                    v-model="start_time2"
                    type="month"
                    placeholder="选择月份">
                </el-date-picker>
                <el-button type="primary" plain size="small" @click="change_month" >切换月份</el-button>
            </p> -->

            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="240" align="left">
                        <template slot-scope="scope">
                            <!-- <el-button type="primary" plain size="mini" v-loading="exportLoading" @click="download_achievement(scope.row)">导出</el-button> -->
                            <el-button class="btn-mar" size="mini" type="primary">
                                <router-link
                                        :to="{
                                            path: 'advertising_index',
                                            query: {
                                                id: scope.row.advertiser_id,
                                            }
                                        }"
                                    >
                                    每日明细
                                </router-link>
                            </el-button>
                            <!-- <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">明细</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button>
                            <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->
                        </template>
                    </el-table-column>
                </template>
            </qa-table>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen" :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData" :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>
    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },

    data() {
        return {
            advertiser_arr: [],
            // table结构
            columns: [
                // {
                //     visible: true,
                //     label: 'id',
                //     prop: 'id',
                // },
                {
                    visible: true,
                    label: '广告账户',
                    prop: 'advertiser_id',
                },
                {
                    visible: true,
                    label: '名称',
                    prop: 'advertiser_name',
                },
                {
                    visible: true,
                    label: '花费',
                    prop: 'spend',
                },
                {
                    visible: true,
                    label: '统计月份',
                    prop: 'month',
                },
                // {
                //     visible: true,
                //     label: '店铺类型',
                //     prop: 'type',
                //     component: 'QuickAdminTag',
                //     formatter: (prop, row) => {
                //         return row.type
                //     },
                // },
            ],
            // 接口地址
            api: {
                index: `/admin/advaccount/index`,
                find: `/admin/advaccount/find`,
                add: `/admin/advaccount/add`,
                edit: `/admin/advaccount/edit`,
                delete: `/admin/advaccount/delete`,
                export: `/admin/advaccount/export`,
                status: `/admin/advaccount/status`,
                change_month: `/admin/advaccount/change_month`,
            },
            editFormParams: {},
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
            //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                id: { value: undefined, op: '=' },
                admin_id: { value: undefined, op: '=' },
                site_id: { value: undefined, op: '=' },
                advertiser_name: { value: undefined, op: '%like%' },
                datetime: { value: [], op: 'range' },
                advertiser_id: { value: [], op: '=' },
                createtime: { value: [], op: 'range' },
                updatetime: { value: [], op: 'range' },
                month: { value:undefined, op: '='},
            },
            start_time2:'',
        }
    },
    created() {
        this.initIndex()
        this.advertiser();
        
    },
    watch: {},
    methods: {
        async advertiser() {
            let response = await this.request.post('/admin/advaccount/advertiser', {})
            this.advertiser_arr = response;
        },
        // 导出
        handleExport () {
            // 只导出已勾选的项
            const _columns = this.columns.filter(item => item.visible)
            const fields = _columns.map((item) => {
                return { field: item.prop, comment: item.label }
            })

            const qyparams = {
                ...this.pageInfo,
                ...this.formatQueryParams(this.queryParams),
                fields: JSON.stringify(fields)
            }

            this.$confirm('是否确认导出所有数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    this.exportLoading = true
                    return this.request.get(this.api.export, { params: qyparams, responseType: "blob" })
                })
                .then(response => {
                    this.download(response, '广告统计.xlsx')
                })
                .catch(err => {})
                .finally(() => {
                    this.exportLoading = false
                })
        },
        change_month() {
            this.$confirm(`确定要切换月份吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest_change_month({ start_time: this.start_time2})
                })
                .catch(err => {})
        },
        baseRequest_change_month({ start_time }) {
            this.loading = true
            this.request
                .post(this.api.change_month, { start_time })
                .then(response => {
                    if(response.code == 1){
                        this.$message.success('切换成功');
                    }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                })
        },
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },
        download_achievement(row) {
            console.log('testrow',row);
            return false;
            this.loading = true
            const qyparams = {
                day_changes : this.day_changes,
                site_id : row.site_id,
                start_time : start_time,
                end_time : end_time
            }
            this.$confirm(`确定要下载广告明细吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    return this.request.get('/admin/businessapi/download_achievement', { params: qyparams, responseType: 'blob' })
                })
                .then(response => {
                    this.download(response,'广告明细.xlsx')
                    this.$message.success('下载成功');
                })
                .catch(err => {})
                .finally(() => {
                    this.loading = false
                })
        },
    },
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
